package JDBC_student_table;

import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: yang
 * Date: 2022-09-01
 * Time: 18:56
 */
public class JDBC_UPDATE {
    public static void main(String[] args) throws SQLException {
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource)dataSource).setURL("jdbc:mysql://127.0.0.1:3306/bite?characterEncoding=UTF-8&useSSL=false");
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("1100ppqqaall");

        Connection connection = dataSource.getConnection();
        Scanner scan = new Scanner(System.in);
        int oldId,id;
        String name;
        System.out.println("请输入你要修改的学生id:");
        oldId = scan.nextInt();
        System.out.println("该学生id修改为:");
        id = scan.nextInt();
        System.out.println("该学生姓名修改为:");
        name = scan.next();
        String sql = "update student set id = ?,name = ? where id = ?;";
        PreparedStatement statement = connection.prepareStatement(sql);
        statement.setInt(1,id);
        statement.setString(2,name);
        statement.setInt(3,oldId);

        statement.executeUpdate();

        statement.close();
        connection.close();

    }
}
